package com.seuic.hid_demo.activity

import android.bluetooth.BluetoothProfile
import android.content.Intent
import android.provider.Settings
import android.view.View
import android.widget.AdapterView
import android.widget.SimpleAdapter
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.seuic.hid_demo.Constans
import com.seuic.hid_demo.R
import com.seuic.hid_demo.base.BaseActivity
import com.seuic.hid_demo.util.ext.hidManager
import kotlinx.android.synthetic.main.activity_main.*
import java.nio.charset.Charset


/**
 * @Desc
 * @author KP
 * @date 2020/12/17 9:46
 * @version
 */

class MainActivity : BaseActivity() {
    override fun getContentLayout(): Int = R.layout.activity_main

    override fun initView() {
        auto_send_cb.isChecked = SPUtils.getInstance().getBoolean(Constans.autoSendKey, false)

        encoding_sp.adapter = object : SimpleAdapter(
            this,
            mutableListOf(
                mapOf(
                    "key0" to Constans.encodings[0],
                ),
                mapOf(
                    "key0" to Constans.encodings[1],
                ),
                mapOf(
                    "key0" to Constans.encodings[2],
                ),
            ),
            R.layout.support_simple_spinner_dropdown_item,
            arrayOf("key0"),
            intArrayOf(android.R.id.text1),
        ) {}

        SPUtils.getInstance().getString(Constans.encodingKey, Constans.encodings[0]).also {
            encoding_sp.setSelection(Constans.encodings.indexOfFirst { s -> s == it })
        }

    }

    override fun initData() {
        hidManager.connectState.observe(this) {
            when (it) {
                BluetoothProfile.STATE_CONNECTED -> state_tv.text = "状态:已连接"
                BluetoothProfile.STATE_CONNECTING -> state_tv.text = "状态:连接中..."
                BluetoothProfile.STATE_DISCONNECTED -> state_tv.text = "状态:已断开"
                BluetoothProfile.STATE_DISCONNECTING -> state_tv.text = "状态:断开中..."
            }
        }
    }

    override fun initAction() {
        encoding_sp.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                SPUtils.getInstance().put(Constans.encodingKey, Constans.encodings[position])
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

        }
        auto_send_cb.setOnCheckedChangeListener { _, isChecked ->
            SPUtils.getInstance().put(Constans.autoSendKey, isChecked)
        }
        connect_btn.setOnClickListener {
            startActivity(Intent(Settings.ACTION_BLUETOOTH_SETTINGS))
        }
        send_btn.setOnClickListener {
            if (hidManager.isConnected) {
                code_tv.text.trim().toString().takeIf { it.isNotBlank() }?.run {
                    when (encoding_sp.selectedItemPosition) {
                        0 -> hidManager.sendText(
                            this,
                            Charset.defaultCharset(),
                            true
                        )
                        1 -> hidManager.sendText(
                            this,
                            Charset.forName("GBK")
                        )
                        2 -> hidManager.sendText(this, Charsets.UTF_8)
                        else -> ""
                    }
                }
            } else {
                ToastUtils.showShort("HID设备未连接！")
            }
        }

    }

    @BusUtils.Bus(
        tag = Constans.EVENT_TAG_SCAN,
        sticky = true,
        threadMode = BusUtils.ThreadMode.MAIN,
        priority = 1000
    )
    fun receiver(code: String) {
        if (!isFinishing && !isDestroyed)
            code_tv.setText(code)
    }
}